<template>
  <div :id="chartID" style="width: 100%;height: 100%;" />
</template>

<script>
import echarts from 'echarts'
import resize from '@/components/Charts//mixins/resize'
export default {
  mixins: [resize],
  props: {
    dataList: {
      type: Array,
      default: () => {
        return [
          {
            loadTime: '',
            loadAvg: 0
          }
        ]
      }
    }
  },
  data() {
    return {
      chart: null,
      chartID: 'systemLoad',
      xColumnItems: [],
      yColumnItems: []
    }
  },
  watch: {
    dataList() {
      this.initChart()
      const x = []
      const y = []
      if (this.dataList) {
        this.dataList.forEach(item => {
          x.push(item.loadTime)
          y.push(item.loadAvg.toFixed(4))
        })
      }
      this.xColumnItems = x
      this.yColumnItems = y
      this.initChart()
    }
  },
  mounted() {
    this.initChart()
  },
  created() {
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      const optionData = {
        grid: {
          left: '3%', // 默认10%
          right: '4%', // 默认10%
          bottom: '8%', // 默认60
          top: '5%',
          containLabel: true
          // grid区域是否包含坐标轴的刻度标签
        },
        tooltip: {
          trigger: 'axis',
          position: function(pt) {
            return [pt[0], '10%']
          }
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: this.xColumnItems
        },
        yAxis: {
          // type: 'value',
          // boundaryGap: [0, '100%']
        },
        series: [
          {
            name: '负载',
            type: 'line',
            symbol: 'none',
            sampling: 'lttb',
            itemStyle: {
              color: 'rgb(255, 70, 131)'
            },
            areaStyle: {
              color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                  offset: 0,
                  color: 'rgb(255, 158, 68)'
                },
                {
                  offset: 1,
                  color: 'rgb(255, 70, 131)'
                }
              ])
            },
            data: this.yColumnItems
          }
        ]
      }

      this.chart = echarts.init(document.getElementById(this.chartID))
      this.chart.setOption(optionData, true)
    }
  }
}
</script>
